package com.sptpc.fragmenttryteach;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTabHost;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;

import com.sptpc.fragmenttryteach.UI.FirstFragment;
import com.sptpc.fragmenttryteach.UI.SecondFragment;
import com.sptpc.fragmenttryteach.UI.ThirdFragment;

public class MainActivity extends FragmentActivity {
    //三个fragment
    private Fragment f1;
    private Fragment f2;
    private Fragment f3;

    //底部三个按钮
    private Button foot1;
    private Button foot2;
    private Button foot3;

    //TabHost
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        foot1 = (Button) findViewById(R.id.btn1);
        foot2 = (Button) findViewById(R.id.btn2);
        foot3 = (Button) findViewById(R.id.btn3);

//
//        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("标签一"),FirstFragment.class,null);
//        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("标签二"),SecondFragment.class,null);
//        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("标签三"),ThirdFragment.class,null);
        f1 = new FirstFragment();
        f2 = new SecondFragment();
        f3 = new ThirdFragment();
//        getSupportFragmentManager().beginTransaction().add(R.id.container,new FirstFragment()).commit();
        foot1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.container,f1,"1")
                        .addToBackStack("1")
                        .commit();
            }
        });
        foot2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.container,f2,"2")
                        .addToBackStack("2")
                        .hide(f1)
                        .commit();
            }
        });
        foot3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.container,f3,"3")
                        .addToBackStack("3")
                        .commit();
            }
        });

    }
}
